package com.example.administrator.webviewtest;

import android.util.Log;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpClientStack;
import com.android.volley.toolbox.HttpStack;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.ta.utdid2.android.utils.StringUtils;

import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.DefaultHttpClient;


/**
 * Created by Administrator on 2015/12/3.
 * 关海写的
 */
public class ShopApi_1 {
    //public final static String HOST = "192.168.2.112:8080/shop";
    public final static String HOST = "www.klg100.com.cn";
    public static String API_URL = "http://" + HOST;
    public static String FILE_URL = "http://" + HOST + "/";


    static RequestQueue mQueue = null;// Volley.newRequestQueue(BaseApplication.getContext());


    //获取商品评价信息
    public static void queryComment(String itemId, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/item/comment?rows=3&id=" + itemId,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //获取商品详情
    public static void queryDesc(String itemId, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/item/ajax/desc?id=" + itemId,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //加入购物车
    public static void addCart(String itemId, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, API_URL + "/app/v1/cart/add?number=1&itemId=" + itemId,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //获取活动商品
    public static void queryActivityItem(String id, int page, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/activity?id=" + id + "&page=" + page,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //获取活动商品（分类）
    public static void queryActivityItem_1(String id, int page, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/cate?id=" + id + "&page=" + page,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //提交订单
    public static void addOrder(String str, Response.Listener<String> listener) {
        String oi = str.replaceAll("#", "%23"); //# 需要转移为%23 ，不然会报错
        StringRequest stringRequest = new StringRequest(Request.Method.POST, API_URL + "/app/v1/order/add" + oi,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //订单详细
    public static void OrderDetail(String itemId, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/order/detail?id=" + itemId,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //品牌
    public static void queryBrand(Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/brand/newlist",
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //品牌商品
    public static void queryBrandItem(String brand, String sidx, String sord, int page, Response.Listener<String> listener) {
        String url = API_URL + "/app/v1/brand/item?brand=" + brand + "&page=" + page;
        if (!StringUtils.isEmpty(sidx) && !StringUtils.isEmpty(sord)) {
            url = url + "&sord=" + sord + "&sidx=" + sidx;
        }
        StringRequest stringRequest = new StringRequest(url,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //我的红包（需要权限）
    public static void queryMyEnvelope(Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/package/list/recive",
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //领取红包
    public static void reciveRedEnvelope(String str, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/package/recive" + str,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //发送红包account,money,
    public static void sendRedEnvelope(String account, String money, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/package/send?account=" + account + "&money=" + money,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }


    //确认收货
    public static void recive(String id, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/order/recive?id=" + id,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //判断是否有发红包权限
    public static void isSendRed(Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/security/isSendRed",
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //获取代金券金额
    public static void getBlance(Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/member/blance",
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    //
    public static void queryAjax(Integer offset, Integer maxResult, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/index/ajaxList?offset=" + offset + "&maxResult=" + maxResult,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }


    public static void change_pwd(String password, String newPassword, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/member/password/change?password=" + password + "&newPassword=" + newPassword,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }


    /**
     * 获取登录相关的安全信息
     *
     * @param url      app 要跳转的位置？  这个参数待定
     * @param listener
     */
    public static void pre_login(Response.Listener<String> listener, String url) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/security/pre_login",
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }

    /**
     * 根据收货地址id 获取收货地址详细， 用于修改收货地址的时候加载出地址详细信息
     *
     * @param addressId
     */
    public static void getAddress(String addressId, Response.Listener<String> listener) {
        StringRequest stringRequest = new StringRequest(API_URL + "/app/v1/member/address/detail?id=" + addressId,
                listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });
        getRequestQueue().add(stringRequest);
    }


    public static RequestQueue getRequestQueue() {
        // if (mQueue == null) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        //非持久化存储(内存存储) BasicCookieStore | 持久化存储 PreferencesCookieStore
        CookieStore cookieStore = new PreferencesCookieStore(BaseApplication.getContext());
        httpclient.setCookieStore(cookieStore);
        HttpStack httpStack = new HttpClientStack(httpclient);
        mQueue = Volley.newRequestQueue(BaseApplication.getContext(), httpStack);
        //  }

        return mQueue;
    }


}
